package com.example.server.util;

import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;

public class Util {

    public static String savePicture(MultipartFile multipartFile,String directory){
        File path = new File(directory);
        String extension = StringUtils.getFilenameExtension(multipartFile.getOriginalFilename());
        String filename = UUID.randomUUID() + "." + extension;

        try {
            multipartFile.transferTo(new File(path,filename));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return filename;
    }

    public static String savePicture(MultipartFile[] multipartFiles,String directory){
        String[] filenames = new String[multipartFiles.length];
        for(int i = 0; i < multipartFiles.length; i++){
            filenames[i] = "\"" + savePicture(multipartFiles[i], directory) + "\"";
        }
        return Arrays.toString(filenames);
    }
}
